package com.llh;

import com.alibaba.fastjson.JSON;
import ln.*;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
import weaver.general.Util;

import java.security.InvalidKeyException;

/**
 * @Classname License
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/7/18 0018 14:42
 * @Created by 刘立华
 */
public class License {
    public static void main(String[] args) throws Exception {
        String pathTo ="D:\\weaver_ec9_20211220\\ecology\\license\\E96281F222072F8BF6E730D41B42CB30.license";
        LNParse lnp = new LNParse();
        LNBean ecology9 =getLNBean(pathTo, "ecology9");
        String s = JSON.toJSONString(ecology9);
        System.out.println("s = " + s);
    }
    public static LNBean getLNBean(String licensefilepath, String key) throws Exception {
        byte[] publicKey = Zip.getZipSomeByte(licensefilepath, "publicKey");
        byte[] licenseEncryptKey = Zip.getZipSomeByte(licensefilepath, "licenseEncryptKey");
        byte[] licenseFile = Zip.getZipSomeByte(licensefilepath, "license");
        byte[] licenseFile2 = Zip.getZipSomeByte(licensefilepath, "license2");
        String realPublicKey = "";
        if ("emessage2".equals(key)) {
            realPublicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIJWRm0eoQNEgZB9aUlM1PoT0N7cKCBCfkecycpeKeg57e73Fcj4ik9uYrGB01t38ut45iHJi8TLoeORYuUAhWUCAwEAAQ==";
        } else if ("ecology9".equals(key)) {
            realPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyC90YpaWPbLaQwqt3TYlRqYC+gDTivXiVU2ZnL+tVop7tm1Ss8gnXnkd1I0jr2ffQK6m4HIGdz4lyxOfJVuT9hwtDpnflxK5fBIpc6N5iB3bZkes3XMJTyXY+afvh7vKf9yW0p1ZgQkMp7Ty4nRNQ1H/JV7RIUohEM24udiZNZySLpIYeAxTl8gR/EKL/YCIxBQfFEyQtijB0+X6Sfd/CWgNGVPuPr8V5nUZm8vXIszWBSPamD/yfvwNI9PAOII7OBNMXOC9BFAjTdCKkxdRS4ovu2V9STxAu0P8hhTnH0/zpxi4VOn32povh4f5J7x5eV+vSaN5G1G1zVPs5lc62QIDAQAB";
        }

        String publicKeyStr = new String(Base64.encodeBase64(publicKey));
        if (!realPublicKey.equals(publicKeyStr)) {
            throw new Exception("license error!");
        } else {
            JSONObject jsonLicense;
            try {
                byte[] licenseKey = RSACoder.decryptByPublicKey(licenseEncryptKey, publicKey);
                jsonLicense = new JSONObject(new String(DESCoder.decrypt(licenseFile, licenseKey)));
            } catch (InvalidKeyException var15) {
                var15.printStackTrace();
                byte[] licenseInfo2 = DESCoder.decrypt(licenseFile2, key.getBytes());
                jsonLicense = new JSONObject(new String(licenseInfo2));
            }

            LNBean lnb = new LNBean();
            lnb.setCompanyname(jsonLicense.getString("companyname"));
            lnb.setLicensecode(jsonLicense.getString("licensecode"));
            lnb.setHrmnum(jsonLicense.getString("hrmnum"));
            lnb.setExpiredate(jsonLicense.getString("expiredate"));
            lnb.setConcurrentFlag(jsonLicense.getString("concurrentFlag"));
            lnb.setLicense(jsonLicense.getString("license"));

            try {
                lnb.setCid(jsonLicense.getString("cid"));
            } catch (Exception var14) {
                System.out.println(var14);
            }

            try {
                lnb.setScType(jsonLicense.getString("scType"));
            } catch (Exception var13) {
                System.out.println(var13);
            }

            try {
                lnb.setScCount(jsonLicense.getString("scCount"));
            } catch (Exception var12) {
                System.out.println(var12);
            }

            return lnb;
        }
    }

}